{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "07f2b5a0-6e71-4333-8164-4fcc0c1fa780",
   "metadata": {},
   "outputs": [],
   "source": [
    "from openai import OpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "636a239d-cae2-4912-abe5-d1dcb49045e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "client = OpenAI()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b4b6b3cc-4588-4eeb-8603-3ba6ae79b2aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_openai_response(client, prompt, model=\"gpt-3.5-turbo\"):\n",
    "    response = client.chat.completions.create(\n",
    "        model=model,\n",
    "        messages=[{\"role\": \"user\", \"content\": prompt}],\n",
    "    )\n",
    "    return response.choices[0].message.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1a10d3c9-0300-422a-b333-510e5ab17c4c",
   "metadata": {},
   "outputs": [],
   "source": [
    "q1 = \"我刚买的XYZ智能手表无法同步我的日历，我应该怎么办？\"\n",
    "q2 = \"XYZ手表的电池可以持续多久？\"\n",
    "q3 = \"XYZ品牌的手表和ABC品牌的手表相比，有什么特别的功能吗？\"\n",
    "q4 = \"安装XYZ智能手表的软件更新后，手表变得很慢，这是啥原因？\"\n",
    "q5 = \"XYZ智能手表防水不？我可以用它来记录我的游泳数据吗？\"\n",
    "q6 = \"我想知道XYZ手表的屏幕是什么材质，容不容易刮花？\"\n",
    "q7 = \"请问XYZ手表标准版和豪华版的售价分别是多少？还有没有进行中的促销活动？\"\n",
    "q_list = [q1, q2, q3, q4, q5, q6, q7]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "06f6a221-f280-4352-a176-ccd1f252928c",
   "metadata": {},
   "outputs": [],
   "source": [
    "category_list = [\"产品规格\", \"使用咨询\", \"功能比较\", \"用户反馈\", \"价格查询\", \"故障问题\", \"其它\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4b722274-7db9-4d2f-af00-8c32c2df7624",
   "metadata": {},
   "outputs": [],
   "source": [
    "classify_prompt_template = \"\"\"\n",
    "你的任务是为用户对产品的疑问进行分类。\n",
    "请仔细阅读用户的问题内容，给出所属类别。类别应该是这些里面的其中一个：{categories}。\n",
    "直接输出所属类别，不要有任何额外的描述或补充内容。\n",
    "用户的问题内容会以三个#符号进行包围。\n",
    "\n",
    "###\n",
    "{question}\n",
    "###\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "bb4d903b-5c0d-49f6-96d0-a0b6bc713e75",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "故障问题\n",
      "产品规格\n",
      "功能比较\n",
      "故障问题\n",
      "产品规格\n",
      "产品规格\n",
      "价格查询\n"
     ]
    }
   ],
   "source": [
    "for q in q_list:\n",
    "    formatted_prompt = classify_prompt_template.format(categories=\"，\".join(category_list), question=q)\n",
    "    response = get_openai_response(client, formatted_prompt)\n",
    "    print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "df8d3a02-ffcf-482d-8965-b80144e11499",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
